﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class fengcai_UploadPhoto : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        string fileName = string.Empty;

        string msg = UploadFile(this.FileUpload1, ref fileName);
        if (!string.IsNullOrEmpty(msg))
        {
            Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "script", "alert('" + msg + "');", true);
            return;
        }

        string title = ValidationHelper.ReplaceSQLKeyWord(this.txtTitle.Text);
        string remark = ValidationHelper.ReplaceSQLKeyWord(this.txtRemark.Text);

        AccessHelper.excuteSql("insert into fengcai (imagesrc,title,remark,isshenhe,isdeleted,inserttime) values ('"
            + fileName + "','"
            + title + "','"
            + remark + "',false,false,'"
            + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')");
        Page.ClientScript.RegisterClientScriptBlock(this.Page.GetType(), "script", "alert('图片上传成功！');", true);
        this.txtTitle.Text = "";
        this.txtRemark.Text = "";
    }

    #region 上载文件
    /// <summary>
    /// 上载文件
    /// </summary>
    /// <param name="fileUpload"></param>
    /// <param name="file"></param>
    /// <returns></returns>
    public String UploadFile(FileUpload fileUpload, ref String file)
    {
        String Message = String.Empty;
        file = DateTime.Now.ToString("yyyyMMddHHmmss")
            + (new Random()).Next(10000).ToString("D5")
            + fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf("."));
        string path = HttpContext.Current.Server.MapPath("") + "\\uploadImages\\" + file;

        HttpFileCollection files = HttpContext.Current.Request.Files;
        String fileName = System.IO.Path.GetFileName(fileUpload.FileName);
        HttpPostedFile postedFile = files[0];

        if (!String.IsNullOrEmpty(fileName))
        {
            //获取文件后缀名
            String str = fileName.Substring(fileName.Length - 4, 4).ToLower();
            if (str == ".jpg" || str == "jpeg" || str == ".bmp" || str == ".gif" || str == ".png")
            {
                try
                {
                    //上传文件
                    postedFile.SaveAs(path);

                    //消息处理
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            }
            else
            {
                //请确认上传的是Excel文件
                //Message= "只能上传.xls文件";
                Message = "只能上传图片！";
            }
        }
        else
        {
            //请选择要上传的文件
            //Message= "请选择要上传的文件！";
            Message = "请选择要上传的图片！";
        }
        return Message;
    }
    #endregion
}